// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        shoot_time: 0.2,    //  射击间隔时间
        bullet_prefab: {
            type: cc.Prefab,
            default: []
        },
        bullet_root: {
            type: cc.Node,
            default: null
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.ulevel = 1
        this.nowtime = 0
    },
    
    enterFireMode () {
        this.isFireMode = true
    },

    leaveFireMode () {
        this.isFireMode = false
    },

    doShoot () {
        var bullet = cc.instantiate(this.bullet_prefab[this.ulevel - 1])
        this.bullet_root.addChild(bullet)

        var pos = this.node.getPosition()
        pos.y += 152
        bullet.setPosition(pos)
    },

    update (dt) {
        if (!this.isFireMode) {
            return
        }
        this.nowtime += dt
        if (this.nowtime > this.shoot_time) {
            this.doShoot()
            this.nowtime = 0
        }
    },

    doMove (offset, parent) {
        if (Math.abs(this.node.x + offset.x) < parent.width / 2) {
            this.node.x += offset.x
        }
        if (Math.abs(this.node.y + offset.y) < parent.height / 2) {
            this.node.y += offset.y
        }
        // console.log('plane.doMove:', this.node.x, this.node.y)
    }
});
